<template>
  <CollapseContainer title="基本设置" :canExpan="false">
    <a-row :gutter="24">
      <a-col :span="6">
        LOGO
      </a-col>
      <a-col :span="18" v-if="websiteDetail" >
        <div class="change-avatar">
          <CropperAvatar
            :uploadApi="uploadApi"
            :value="websiteDetail.logo"
            btnText="更换LOGO"
            :btnProps="{ preIcon: 'ant-design:cloud-upload-outlined' }"
            @change="updateAvatar"
            width="150"
          />
        </div>
      </a-col>
    </a-row>
    <a-row :gutter="24">
      <a-col :span="24">
        <BasicForm @register="register" />
      </a-col>
    </a-row>
    <Button type="primary" @click="handleSubmit"> 更新基本信息 </Button>
  </CollapseContainer>
</template>
<script lang="ts">
  import { Button, Row, Col } from 'ant-design-vue';
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { CollapseContainer } from '/@/components/Container';
  import { CropperAvatar } from '/@/components/Cropper';

  import { useMessage } from '/@/hooks/web/useMessage';

  // import headerImg from '/@/assets/images/header.jpg';
  import { detailByCompanyId, saveOrUpdate } from '/@/api/company/website';
  import { WebsiteResult } from '/@/api/company/websiteModel';
  import { baseSetschemas } from './data';
  // import { useUserStore } from '/@/store/modules/user';
  import { uploadApi } from '/@/api/sys/upload';

  export default defineComponent({
    components: {
      BasicForm,
      CollapseContainer,
      Button,
      ARow: Row,
      ACol: Col,
      CropperAvatar,
    },
    setup() {
      const { createMessage } = useMessage();
      // const userStore = useUserStore();
      let websiteDetail = ref<WebsiteResult>();

      const [register, { validate, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas: baseSetschemas,
        showActionButtonGroup: false,
      });

      /*onMounted(async () => {
        const data = await accountInfoApi();
        setFieldsValue(data);
      });*/

      async function detailByCid() {
        const result = await detailByCompanyId();
        console.log("详情", result);
        websiteDetail.value = result;
        setFieldsValue(result);
      }

      onMounted(async () => {
        detailByCid();
      });

      /*const avatar = computed(() => {
        const { avatar } = userStore.getUserInfo;
        return avatar || headerImg;
      });*/
      /**
       * 更新logo
       */
      function updateAvatar(src: string, data: any) {
        websiteDetail.value!.logo = data.result;
      }

      return {
        websiteDetail,
        register,
        uploadApi: uploadApi as any,
        updateAvatar,
        handleSubmit: async () => {
          const values = await validate();
          if(websiteDetail.value!.logo){
            values.logo = websiteDetail.value!.logo;
          }
          const result = await saveOrUpdate(values);
          createMessage.success('更新成功！');
          console.log("结果",result);
          detailByCid();
          // createMessage.success('更新成功！' + JSON.stringify(values));
        },
      };
    },
  });
</script>

<style lang="less" scoped>
  .change-avatar {
    img {
      display: block;
      margin-bottom: 15px;
      border-radius: 50%;
    }
  }
</style>
